<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use Curl\Curl;
use think\Config;
use think\Request;

/**
 * 政策
 * Class Industry
 * @package app\index\controller
 */
class Policy extends BaseController
{
    /**
     * 政策列表
     */
    public function policyLibraryList(){
        $reqData['industry_id'] = $this->request->get('industry_id');
        $reqData['city_id'] = $this->request->get('city_id');
        $reqData['chain_id'] = $this->request->get('chain_id');
        $reqData['level'] = $this->request->get('level');
        $city_name= $this->request->get('city_name','');

        $identification = $this->request->get('identification');
        $keywords = $this->request->get('keywords','');
        $method = $this->request->method();
        $page = $this->request->get('page',0);
        $limit = $this->request->get('limit',10);
        if ($reqData['city_id']==1){
            $reqData['city_id']=0;
        }
        // 业务
        $request_arr = ['level'=> $reqData['level'],'page'=>$page,'limit'=>$limit,'industry_id' => $reqData['industry_id'],'city_id' => $reqData['city_id'], 'identification' => $identification, 'chain_id'=> $reqData['chain_id'], 'keywords'=>$keywords ];
        $curl_data = $this->getBusiness('Common')->curl_request_data($method,$request_arr);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
//
//        // 数据分页返回
//        $offset = $page*$limit;
//        $res = array_slice($curl_data['data']['data']['data'],$offset,$limit);
//        $curl_data['data']['data']['data'] = $res;

        $this->result['status'] = 1;
        //$this->result['data'] = $curl_data;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 政策详情
     * @return false|string
     */
    public function policyLibraryDetail()
    {
        $reqData['identification'] = $this->request->get('identification');
        $reqData['method'] = $this->request->method();
        if (!$reqData['identification']){
            $this->result['status'] = -1;
            $this->result['msg'] = '参数错误';
            return json_encode($this->result);
        }

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('identification' => $reqData['identification']));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 根据城市获取政策
     * @return false|string
     */
    public function getCityPolicy()
    {
        $reqData['chain_id'] = $this->request->get('chain_id');
        $reqData['method'] = $this->request->method();
        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('chain_id' => $reqData['chain_id']));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        $arr = array();
        foreach ($curl_data['data']['data'] as $val){
            $arr[$val['province_id']]['list'][] = $val;
        }
        array_walk($arr,array($this,'cityPolicyCallback'));
        $this->result['status'] = 1;
        $this->result['data'] = $arr;
        return json_encode($this->result);
    }

    public function cityPolicyCallback(&$val,$key)
    {
        $count = count($val['list']);
        $val['total'] = $count;
    }

    public function cityDiffPolicy()
    {
        $reqData['city_id'] = $this->request->get('city_id',0,'intval');
        $reqData['other_city_id'] = $this->request->get('other_city_id',0,'intval');
        $reqData['chain_id'] = $this->request->get('chain_id',0,'intval');
        $reqData['type'] = $this->request->get('type',0,'intval');
        $reqData['compare_type']= $this->request->get('compare_type','','trim');
        $method = $this->request->method();

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($method ,array('compare_type'=>$reqData['compare_type'],'city_id' => $reqData['city_id'],'other_city_id'=>$reqData['other_city_id'],'chain_id'=>$reqData['chain_id'],'type'=>$reqData['type']));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        $this->result['status'] = 1;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }
}